package com.youlai.boot.gym.controller;

import com.youlai.boot.common.model.Option;
import com.youlai.boot.gym.service.ClassTypesService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.gym.model.form.ClassTypesForm;
import com.youlai.boot.gym.model.query.ClassTypesQuery;
import com.youlai.boot.gym.model.vo.ClassTypesVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

import java.util.List;

/**
 * 课程类型前端控制层
 *
 * @author hxc
 * @since 2025-06-01 10:41
 */
@Tag(name = "课程类型接口")
@RestController
@RequestMapping("/api/v1/class-types")
@RequiredArgsConstructor
public class ClassTypesController  {

    private final ClassTypesService classTypesService;

    @Operation(summary = "课程类型分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('gym:classTypes:query')")
    public PageResult<ClassTypesVO> getClassTypesPage(ClassTypesQuery queryParams ) {
        IPage<ClassTypesVO> result = classTypesService.getClassTypesPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "课程类型下拉列表")
    @GetMapping("/options")
    @PreAuthorize("@ss.hasPerm('gym:classTypes:query')")
    public Result<List<Option<Long>>> getDeptOptions() {
        List<Option<Long>> list = classTypesService.listTypeOptions();
        return Result.success(list);
    }

    @Operation(summary = "新增课程类型")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('gym:classTypes:add')")
    public Result<Void> saveClassTypes(@RequestBody @Valid ClassTypesForm formData ) {
        boolean result = classTypesService.saveClassTypes(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取课程类型表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('gym:classTypes:edit')")
    public Result<ClassTypesForm> getClassTypesForm(
        @Parameter(description = "课程类型ID") @PathVariable Long id
    ) {
        ClassTypesForm formData = classTypesService.getClassTypesFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改课程类型")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('gym:classTypes:edit')")
    public Result<Void> updateClassTypes(
            @Parameter(description = "课程类型ID") @PathVariable Long id,
            @RequestBody @Validated ClassTypesForm formData
    ) {
        boolean result = classTypesService.updateClassTypes(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除课程类型")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('gym:classTypes:delete')")
    public Result<Void> deleteClassTypess(
        @Parameter(description = "课程类型ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = classTypesService.deleteClassTypess(ids);
        return Result.judge(result);
    }
}
